package com.atguigu.chapter05.transform;

import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

/**
 * Author: Pepsi
 * Date: 2023/7/27
 * Desc:
 */
public class Flink04_keyBy {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port",1000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(2);

        env.fromElements(1,2,3,4,5,6)
                .keyBy(new KeySelector<Integer, String>() {
                    @Override
                    public String getKey(Integer ele) throws Exception {
                        if (ele%2==0){
                            return "偶数";
                        }else {
                            return "奇数";
                        }
                    }
                })
                .print();

        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
